package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.entity.ProjectMsgTemplateDO;
import com.ruoyi.system.mapper.ProjectMsgTemplateMapper;
import com.ruoyi.system.service.ProjectMsgTemplateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProjectMsgTemplateServiceImpl extends ServiceImpl<ProjectMsgTemplateMapper, ProjectMsgTemplateDO> implements ProjectMsgTemplateService {

    @Resource
    private ProjectMsgTemplateMapper mapper;

    @Override
    public void bind(Long projectId, Long templateId, Integer type, String operator) {
        if (projectId == null || templateId == null || type == null) {
            throw new ServiceException("绑定参数不完整");
        }
        // 停用历史启用绑定
        LambdaUpdateWrapper<ProjectMsgTemplateDO> uw = new LambdaUpdateWrapper<>();
        uw.eq(ProjectMsgTemplateDO::getProjectId, projectId)
          .eq(ProjectMsgTemplateDO::getType, type)
          .eq(ProjectMsgTemplateDO::getIsActive, 1)
          .eq(ProjectMsgTemplateDO::getIsDeleted, 0)
          .set(ProjectMsgTemplateDO::getIsActive, 0)
          .set(ProjectMsgTemplateDO::getUpdateBy, operator);
        this.update(uw);

        // 新建启用绑定
        ProjectMsgTemplateDO rec = new ProjectMsgTemplateDO();
        rec.setProjectId(projectId);
        rec.setTemplateId(templateId);
        rec.setType(type);
        rec.setIsActive(1);
        rec.setIsDeleted(0);
        rec.setCreateBy(operator);
        rec.setUpdateBy(operator);
        this.save(rec);
    }

    @Override
    public void disable(Long projectId, Integer type, String operator) {
        if (projectId == null || type == null) {
            throw new ServiceException("停用参数不完整");
        }
        LambdaUpdateWrapper<ProjectMsgTemplateDO> uw = new LambdaUpdateWrapper<>();
        uw.eq(ProjectMsgTemplateDO::getProjectId, projectId)
          .eq(ProjectMsgTemplateDO::getType, type)
          .eq(ProjectMsgTemplateDO::getIsActive, 1)
          .eq(ProjectMsgTemplateDO::getIsDeleted, 0)
          .set(ProjectMsgTemplateDO::getIsActive, 0)
          .set(ProjectMsgTemplateDO::getUpdateBy, operator);
        this.update(uw);
    }

    @Override
    public Long getActiveTemplateId(Long projectId, Integer type) {
        return mapper.selectActiveTemplateId(projectId, type);
    }

    @Override
    public void assertHasActive(Long projectId, Integer type) {
        Long tid = getActiveTemplateId(projectId, type);
        if (tid == null) {
            throw new ServiceException("项目未绑定该模块启用模板，无法发送");
        }
    }

    @Override
    public void assertIsActiveBinding(Long projectId, Integer type, Long templateId) {
        int exists = mapper.existsActiveBinding(projectId, type, templateId);
        if (exists <= 0) {
            throw new ServiceException("所选模板未绑定为该项目该模块的启用模板");
        }
    }

    @Override
    public List<Long> getProjectIdsByTemplateId(Long templateId) {
        if (templateId == null) {
            throw new ServiceException("模板ID不能为空");
        }
        return mapper.selectProjectIdsByTemplateId(templateId);
    }
}


